package com.tourlaid.mamagerment.service.impl

import com.tourlaid.mamagerment.entity.Supplier
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.mapper.SupplierMapper
import com.tourlaid.mamagerment.service.SupplierService
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/21.
 */
@Service("supplierService")
class SupplierServiceImpl : SupplierService {

    @Resource
    val supplierMapper: SupplierMapper? = null

    override fun queryForList(): List<Supplier>? {
        return supplierMapper!!.queryForList()
    }

    override fun insert(supplierName: String?, controlNo: String?) {
        var supplier = this.getByName(supplierName)
        if (supplier != null) {
            throw TldException(TldExceptionEnum.NAME_EXIST)
        }
        var supplier2 = this.getByControlNo(controlNo)
        if (supplier2 != null) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        supplier = Supplier()
        supplier.supplierName = supplierName
        supplier.controlNo = controlNo
        supplier.creator = LoginUtil.userId
        supplier.editor = LoginUtil.userId
        supplierMapper!!.insert(supplier)
    }

    override fun getByName(supplierName: String?): Supplier? {
        if (StringUtils.isEmpty(supplierName)) {
            throw TldException(TldExceptionEnum.NAME_NULL)
        }
        return supplierMapper!!.getByName(supplierName)
    }

    override fun getByControlNo(controlNo: String?): Supplier? {
        if (StringUtils.isEmpty(controlNo)) {
            throw TldException(TldExceptionEnum.CONTROLNO_NULL)
        }
        return supplierMapper!!.getByControlNo(controlNo)
    }

    override fun getById(id: Long?): Supplier? {
        id ?: return null
        var supplier = supplierMapper!!.getById(id)
        return supplier
    }

    override fun update(id: Long?, supplierName: String?, controlNo: String?) {
        id ?: throw TldException(TldExceptionEnum.ID_NULL)
        var oldSupplier = this.getByName(supplierName)
        if (oldSupplier != null && !Objects.equals(id, oldSupplier.id)) {
            throw TldException(TldExceptionEnum.NAME_EXIST)
        }
        var oldSupplier2 = this.getByControlNo(controlNo)
        if (oldSupplier2 != null && !Objects.equals(id, oldSupplier2.id)) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        var newSupplier = Supplier()
        newSupplier.id = id
        newSupplier.supplierName = supplierName
        newSupplier.controlNo = controlNo
        newSupplier.editor = LoginUtil.userId
        supplierMapper!!.update(newSupplier)
    }

    override fun queryByIdList(idList: List<Long?>): List<Supplier> {
        return supplierMapper!!.queryByIdList(idList)
    }

    override fun delete(id: Long?) {
        supplierMapper!!.delete(id, LoginUtil.userId)
    }

    override fun queryAllSupplier(): List<Supplier> {
        return supplierMapper!!.queryAllSupplier()
    }
}